package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Message;
import cn.wolfcode.trip.base.domain.MessageInfo;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.mapper.MessageInfoMapper;
import cn.wolfcode.trip.base.mapper.MessageMapper;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;

    @Autowired
    private TravelContentMapper travelContentMapper;

    @Autowired
    private MessageInfoMapper messageInfoMapper;

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public PageInfo query(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), true, false, null);
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(Travel travel) {
        if (travel.getId() != null) {
            travel.setLastUpdateTime(new Date());
            travelMapper.updateByPrimaryKey(travel);
            travel.getTravelContent().setId(travel.getId());
            travelContentMapper.updateByPrimaryKey(travel.getTravelContent());
        } else {
            // 设置作者
            travel.setAuthor(UserContext.getCurrentUser());
            // 设置创建时间
            travel.setCreateTime(new Date());
            // 设置更新时间
            travel.setLastUpdateTime(new Date());
            // 保存游记信息
            travelMapper.insert(travel);
            // 保存游记内容
            travel.getTravelContent().setId(travel.getId());
            travelContentMapper.insert(travel.getTravelContent());
        }
    }

    @Override
    public Travel getById(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public TravelContent getContentById(Long id) {
        return travelContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateState(Travel travel) {
        Travel newTravel = travelMapper.selectByPrimaryKey(travel.getId());
        newTravel.setState(travel.getState());
        // 构造通知信息内容对象
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setTitle("游记审核结果");
        messageInfo.setMsgType(MessageInfo.TYPE_EXAMINE);
        messageInfo.setTargetId(newTravel.getId());
        // 构造通知对象
        Message message = new Message();
        message.setStatus(Message.STATUS_UNREAD);
        message.setReceiver(newTravel.getAuthor());
        // 如果是发布状态设置发布时间
        if (newTravel.getState().equals(Travel.STATE_RELEASE)) {
            newTravel.setReleaseTime(new Date());
            // 发布的时候同时通知用户游记已被发布
            messageInfo.setContent("您好，您提交的<a href=\"editTravels.html?id="+ travel.getId()
                    + "\">" + newTravel.getTitle() + "</a>已审核通过");
            messageInfoMapper.insert(messageInfo);
            message.setMsgInfo(messageInfo);
            messageMapper.insert(message);
        } else if (newTravel.getState().equals(Travel.STATE_REJECT)) {
            // 拒绝的时候通知用户已被拒绝
            messageInfo.setContent("您好，您提交的<a href=\"editTravels.html?id="+ travel.getId()
                    + ">" + newTravel.getTitle() + "</a>审核不通过");
            messageInfoMapper.insert(messageInfo);
            message.setMsgInfo(messageInfo);
            messageMapper.insert(message);
        } else if (newTravel.getState().equals(Travel.STATE_AUDIT)) {
            // 拒绝的时候通知用户重新审核
            messageInfo.setContent("您好，您提交的<a href=\"editTravels.html?id="+ travel.getId()
                    + ">" + newTravel.getTitle() + "</a>已被重新审核");
            messageInfoMapper.insert(messageInfo);
            message.setMsgInfo(messageInfo);
            messageMapper.insert(message);
        }
        travelMapper.updateState(newTravel);
    }
}
